package com.zhs.springbootsequncedemo;

import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.MessageQueueSelector;
import org.apache.rocketmq.common.consumer.ConsumeFromWhere;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@Getter
@Setter
@Slf4j
@AutoConfigureAfter(OrderMessageListener.class)
public class RocketMqConfig {
    @Value("${spring.rocketmq.nameServer}")
    private String nameServer;
    @Value("${spring.rocketmq.topicName}")
    private String topicName;

    @Autowired
    private OrderMessageListener orderMessageListener;

    @Bean
    public DefaultMQProducer producer() throws MQClientException {
        DefaultMQProducer defaultMQProducer=new DefaultMQProducer("producerGroupName");
        defaultMQProducer.setNamesrvAddr(nameServer);
        defaultMQProducer.start();
        return defaultMQProducer;
    }
    @Bean
    public DefaultMQPushConsumer consumer() throws MQClientException {
        DefaultMQPushConsumer defaultMQPushConsumer=new DefaultMQPushConsumer("consumerGroupName");
        defaultMQPushConsumer.setNamesrvAddr(nameServer);
        defaultMQPushConsumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET);
        defaultMQPushConsumer.subscribe(topicName,"*");
        defaultMQPushConsumer.registerMessageListener(orderMessageListener);
        defaultMQPushConsumer.start();
        return defaultMQPushConsumer;
    }


}
